package newDemo;

/**
 * @Author liangzai
 * @Description:
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.ArrayList;
import java.util.Random;
import java.util.HashMap;
import javax.sound.sampled.*;

public class CompletePlaneWar extends JFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new CompletePlaneWar().setVisible(true);
        });
    }

    public CompletePlaneWar() {
        setTitle("完整版飞机大战");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        GamePanel gamePanel = new GamePanel();
        add(gamePanel);

        pack();
        setLocationRelativeTo(null);
    }
}

class GamePanel extends JPanel implements ActionListener, KeyListener {
    // 游戏状态常量
    private static final int MENU = 0;
    private static final int PLAYING = 1;
    private static final int GAME_OVER = 2;
    private static final int PAUSED = 3;
    private boolean[] keys = new boolean[256]; // 存储按键状态的数组

    // 游戏尺寸
    private static final int WIDTH = 500;
    private static final int HEIGHT = 700;

    // 游戏对象
    private Player player;
    private ArrayList<Enemy> enemies;
    private ArrayList<Bullet> bullets;
    private ArrayList<Explosion> explosions;
    private ArrayList<PowerUp> powerUps;

    // 游戏状态
    private int gameState = MENU;
    private int score = 0;
    private int lives = 3;
    private int level = 1;
    private int enemyCount = 0;
    private int enemiesToNextLevel = 20;

    // 武器系统
    private int weaponType = 1; // 1-单发 2-双发 3-激光
    private long lastShotTime = 0;
    private final long shotDelay = 300; // 射击延迟(ms)

    // 资源
    private HashMap<String, BufferedImage> images;
    private HashMap<String, Clip> sounds;
    private Font gameFont;

    // 游戏循环
    private Timer gameTimer;
    private Random random;

    public GamePanel() {
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(this);

        // 初始化游戏对象
        random = new Random();
        player = new Player(WIDTH/2-25, HEIGHT-100, 50, 50);
        enemies = new ArrayList<>();
        bullets = new ArrayList<>();
        explosions = new ArrayList<>();
        powerUps = new ArrayList<>();
        //KeyEvent e = new KeyEvent();

        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (keyCode >= 0 && keyCode < keys.length) {
                keys[keyCode] = true;
            }

            // 处理特殊按键（不依赖持续按下的）
            switch (keyCode) {
                case KeyEvent.VK_SPACE:
                    if (gameState == PLAYING) {
                        shoot();
                    }
                    break;
                case KeyEvent.VK_P:
                    if (gameState == PLAYING) {
                        gameState = PAUSED;
                    } else if (gameState == PAUSED) {
                        gameState = PLAYING;
                    }
                    break;
                case KeyEvent.VK_ESCAPE:
                    if (gameState == PLAYING || gameState == PAUSED) {
                        gameState = MENU;
                    }
                    break;
                case KeyEvent.VK_R:
                    if (gameState == GAME_OVER) {
                        resetGame();
                        gameState = PLAYING;
                    }
                    break;
            }
        }

        // 键盘释放事件
        @Override
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (keyCode >= 0 && keyCode < keys.length) {
                keys[keyCode] = false;
            }
        }

        // 键盘键入事件（不需要实现但必须存在）
        @Override
        public void keyTyped(KeyEvent e) {
            // 通常不需要处理这个事件
        }

        // 加载资源
        loadResources();

        // 设置游戏计时器
        gameTimer = new Timer(16, this); // ~60 FPS
        gameTimer.start();
    }

    private void loadResources() {
        try {
            // 加载图像
            images = new HashMap<>();
            images.put("player", ImageIO.read(getClass().getResource("/player.png")));
            images.put("enemy1", ImageIO.read(getClass().getResource("/enemy1.png")));
            images.put("enemy2", ImageIO.read(getClass().getResource("/enemy2.png")));
            images.put("enemy3", ImageIO.read(getClass().getResource("/enemy3.png")));
            images.put("bullet", ImageIO.read(getClass().getResource("/bullet.png")));
            images.put("laser", ImageIO.read(getClass().getResource("/laser.png")));
            images.put("powerup", ImageIO.read(getClass().getResource("/powerup.png")));

            // 加载字体
            gameFont = Font.createFont(Font.TRUETYPE_FONT,
                    getClass().getResourceAsStream("/gamefont.ttf")).deriveFont(20f);

            // 加载音效
            sounds = new HashMap<>();
            sounds.put("shoot", AudioSystem.getClip());
            sounds.get("shoot").open(AudioSystem.getAudioInputStream(
                    getClass().getResource("/shoot.wav")));

            sounds.put("explosion", AudioSystem.getClip());
            sounds.get("explosion").open(AudioSystem.getAudioInputStream(
                    getClass().getResource("/explosion.wav")));

            sounds.put("powerup", AudioSystem.getClip());
            sounds.get("powerup").open(AudioSystem.getAudioInputStream(
                    getClass().getResource("/powerup.wav")));

            // 背景音乐可以在单独的线程中循环播放

        } catch (IOException | FontFormatException |
                UnsupportedAudioFileException | LineUnavailableException e) {
            System.err.println("资源加载错误: " + e.getMessage());
            // 如果资源加载失败，使用简单的图形和无声模式
        }
    }



    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D)g;

        // 根据游戏状态绘制不同的界面
        switch (gameState) {
            case MENU:
                drawMenu(g2d);
                break;
            case PLAYING:
                drawGame(g2d);
                break;
            case GAME_OVER:
                drawGameOver(g2d);
                break;
            case PAUSED:
                drawGame(g2d);
                drawPaused(g2d);
                break;
        }
    }

    private void drawMenu(Graphics2D g) {
        // 绘制菜单背景
        g.setColor(new Color(0, 0, 0, 200));
        g.fillRect(0, 0, WIDTH, HEIGHT);

        // 绘制标题
        g.setColor(Color.YELLOW);
        g.setFont(gameFont.deriveFont(48f));
        String title = "飞机大战";
        int titleWidth = g.getFontMetrics().stringWidth(title);
        g.drawString(title, (WIDTH - titleWidth)/2, 150);

        // 绘制菜单选项
        g.setColor(Color.WHITE);
        g.setFont(gameFont.deriveFont(24f));
        g.drawString("按空格键开始游戏", (WIDTH-200)/2, 300);
        g.drawString("按H键查看帮助", (WIDTH-150)/2, 350);
        g.drawString("按ESC退出", (WIDTH-100)/2, 400);
    }

    private void drawGame(Graphics2D g) {
        // 绘制背景(可以添加星空背景等)

        // 绘制玩家
        if (images.containsKey("player")) {
            g.drawImage(images.get("player"), player.getX(), player.getY(),
                    player.getWidth(), player.getHeight(), null);
        } else {
            // 备用简单图形
            g.setColor(Color.BLUE);
            g.fillRect(player.getX(), player.getY(), player.getWidth(), player.getHeight());
        }

        // 绘制敌机
        for (Enemy enemy : enemies) {
            String imgKey = "enemy" + enemy.getType();
            if (images.containsKey(imgKey)) {
                g.drawImage(images.get(imgKey), enemy.getX(), enemy.getY(),
                        enemy.getWidth(), enemy.getHeight(), null);
            } else {
                // 备用简单图形
                g.setColor(enemy.getColor());
                g.fillRect(enemy.getX(), enemy.getY(), enemy.getWidth(), enemy.getHeight());
            }
        }

        // 绘制子弹
        for (Bullet bullet : bullets) {
            String imgKey = bullet.isLaser() ? "laser" : "bullet";
            if (images.containsKey(imgKey)) {
                g.drawImage(images.get(imgKey), bullet.getX(), bullet.getY(),
                        bullet.getWidth(), bullet.getHeight(), null);
            } else {
                // 备用简单图形
                g.setColor(bullet.getColor());
                g.fillRect(bullet.getX(), bullet.getY(), bullet.getWidth(), bullet.getHeight());
            }
        }

        // 绘制爆炸效果
        for (Explosion explosion : explosions) {
            explosion.draw(g);
        }

        // 绘制道具
        for (PowerUp powerUp : powerUps) {
            if (images.containsKey("powerup")) {
                g.drawImage(images.get("powerup"), powerUp.getX(), powerUp.getY(),
                        powerUp.getWidth(), powerUp.getHeight(), null);
            } else {
                // 备用简单图形
                g.setColor(Color.MAGENTA);
                g.fillOval(powerUp.getX(), powerUp.getY(),
                        powerUp.getWidth(), powerUp.getHeight());
            }
        }

        // 绘制HUD(游戏信息)
        g.setColor(Color.WHITE);
        g.setFont(gameFont);
        g.drawString("分数: " + score, 20, 30);
        g.drawString("生命: " + lives, 20, 60);
        g.drawString("等级: " + level, 20, 90);
        g.drawString("武器: " + getWeaponName(), 20, 120);
    }

    private void drawGameOver(Graphics2D g) {
        // 绘制游戏结束界面
        g.setColor(new Color(0, 0, 0, 200));
        g.fillRect(0, 0, WIDTH, HEIGHT);

        g.setColor(Color.RED);
        g.setFont(gameFont.deriveFont(48f));
        String gameOver = "游戏结束";
        int gameOverWidth = g.getFontMetrics().stringWidth(gameOver);
        g.drawString(gameOver, (WIDTH - gameOverWidth)/2, 200);

        g.setColor(Color.WHITE);
        g.setFont(gameFont.deriveFont(24f));
        g.drawString("最终分数: " + score, (WIDTH-150)/2, 280);
        g.drawString("按R键重新开始", (WIDTH-150)/2, 330);
        g.drawString("按ESC返回菜单", (WIDTH-150)/2, 380);
    }

    private void drawPaused(Graphics2D g) {
        // 绘制暂停界面
        g.setColor(new Color(0, 0, 0, 180));
        g.fillRect(0, 0, WIDTH, HEIGHT);

        g.setColor(Color.YELLOW);
        g.setFont(gameFont.deriveFont(48f));
        String paused = "游戏暂停";
        int pausedWidth = g.getFontMetrics().stringWidth(paused);
        g.drawString(paused, (WIDTH - pausedWidth)/2, 300);

        g.setColor(Color.WHITE);
        g.setFont(gameFont.deriveFont(24f));
        g.drawString("按P继续游戏", (WIDTH-120)/2, 350);
    }

    private String getWeaponName() {
        switch (weaponType) {
            case 1: return "单发";
            case 2: return "双发";
            case 3: return "激光";
            default: return "未知";
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (gameState == PLAYING) {
            updateGame();
        }
        repaint();
    }

    private void updateGame() {
        // 更新玩家位置
        updatePlayerPosition();

        // 生成敌机
        spawnEnemies();

        // 更新所有游戏对象
        updateBullets();
        updateEnemies();
        updateExplosions();
        updatePowerUps();

        // 检测碰撞
        checkCollisions();

        // 检查关卡升级
        checkLevelUp();
    }

    private void updatePlayerPosition() {
        int speed = 5;

        // 确保 keys 数组不为 null
        if (keys == null) {
            keys = new boolean[256];
            return;
        }

        // 根据按键状态更新玩家位置
        if (keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A]) {
            player.move(-speed, 0);
        }
        if (keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D]) {
            player.move(speed, 0);
        }
        if (keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W]) {
            player.move(0, -speed);
        }
        if (keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S]) {
            player.move(0, speed);
        }

        // 确保玩家不会移出屏幕
        player.setX(Math.max(0, Math.min(WIDTH - player.getWidth(), player.getX())));
        player.setY(Math.max(0, Math.min(HEIGHT - player.getHeight(), player.getY())));
    }



    private void spawnEnemies() {
        // 根据关卡难度生成不同类型的敌机
        int spawnRate = 50 - level * 2; // 随着等级提高，敌机出现更频繁
        if (random.nextInt(Math.max(10, spawnRate)) == 0) {
            int type;
            if (level < 3) {
                type = 1; // 初级关卡只有普通敌机
            } else if (level < 6) {
                type = random.nextInt(2) + 1; // 中级关卡有普通和快速敌机
            } else {
                type = random.nextInt(3) + 1; // 高级关卡有所有类型敌机
            }

            Enemy enemy;
            switch (type) {
                case 1: // 普通敌机
                    enemy = new NormalEnemy(random.nextInt(WIDTH-40), -40, 40, 40);
                    break;
                case 2: // 快速敌机
                    enemy = new FastEnemy(random.nextInt(WIDTH-30), -30, 30, 30);
                    break;
                case 3: // 大型敌机(需要多次击中)
                    enemy = new BigEnemy(random.nextInt(WIDTH-60), -60, 60, 60, 3);
                    break;
                default:
                    enemy = new NormalEnemy(random.nextInt(WIDTH-40), -40, 40, 40);
            }
            enemies.add(enemy);
        }
    }

    private void updateBullets() {
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            bullet.update();

            // 移除超出屏幕的子弹
            if (bullet.getY() < -bullet.getHeight() || bullet.getY() > HEIGHT ||
                    bullet.getX() < -bullet.getWidth() || bullet.getX() > WIDTH) {
                bullets.remove(i);
                i--;
            }
        }
    }

    private void updateEnemies() {
        for (int i = 0; i < enemies.size(); i++) {
            Enemy enemy = enemies.get(i);
            enemy.update();

            // 移除超出屏幕的敌机
            if (enemy.getY() > HEIGHT) {
                enemies.remove(i);
                i--;
            }
        }
    }

    private void updateExplosions() {
        for (int i = 0; i < explosions.size(); i++) {
            Explosion explosion = explosions.get(i);
            explosion.update();

            // 移除已完成的爆炸效果
            if (explosion.isFinished()) {
                explosions.remove(i);
                i--;
            }
        }
    }

    private void updatePowerUps() {
        for (int i = 0; i < powerUps.size(); i++) {
            PowerUp powerUp = powerUps.get(i);
            powerUp.update();

            // 移除超出屏幕的道具
            if (powerUp.getY() > HEIGHT) {
                powerUps.remove(i);
                i--;
            }
        }
    }

    private void checkCollisions() {
        // 子弹与敌机碰撞
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);

            for (int j = 0; j < enemies.size(); j++) {
                Enemy enemy = enemies.get(j);

                if (bullet.getBounds().intersects(enemy.getBounds())) {
                    // 敌机受伤
                    boolean enemyDestroyed = enemy.hit(bullet.getDamage());

                    // 播放爆炸音效
                    playSound("explosion");

                    // 创建爆炸效果
                    explosions.add(new Explosion(enemy.getX(), enemy.getY(),
                            enemy.getWidth(), enemy.getHeight()));

                    // 如果敌机被摧毁
                    if (enemyDestroyed) {
                        enemies.remove(j);
                        score += enemy.getScoreValue();
                        enemyCount++;

                        // 随机掉落道具
                        if (random.nextInt(5) == 0) { // 20%几率掉落道具
                            powerUps.add(new PowerUp(enemy.getX(), enemy.getY(), 30, 30));
                        }

                        j--; // 调整索引
                    }

                    // 移除子弹(激光除外)
                    if (!bullet.isLaser()) {
                        bullets.remove(i);
                        i--;
                    }

                    break; // 一颗子弹只能击中一个敌机
                }
            }
        }

        // 玩家与敌机碰撞
        for (int i = 0; i < enemies.size(); i++) {
            Enemy enemy = enemies.get(i);

            if (player.getBounds().intersects(enemy.getBounds())) {
                // 玩家受伤
                lives--;

                // 创建爆炸效果
                explosions.add(new Explosion(player.getX(), player.getY(),
                        player.getWidth(), player.getHeight()));

                // 移除敌机
                enemies.remove(i);
                i--;

                // 检查游戏是否结束
                if (lives <= 0) {
                    gameState = GAME_OVER;
                    // 保存高分等逻辑可以在这里添加
                }

                break; // 一次只处理一个碰撞
            }
        }

        // 玩家与道具碰撞
        for (int i = 0; i < powerUps.size(); i++) {
            PowerUp powerUp = powerUps.get(i);

            if (player.getBounds().intersects(powerUp.getBounds())) {
                // 获得道具效果
                applyPowerUp(powerUp);

                // 播放音效
                playSound("powerup");

                // 移除道具
                powerUps.remove(i);
                i--;
            }
        }
    }

    private void applyPowerUp(PowerUp powerUp) {
        // 随机获得一种强化效果
        int effect = random.nextInt(3) + 1;

        switch (effect) {
            case 1: // 生命恢复
                lives = Math.min(3, lives + 1);
                break;
            case 2: // 武器升级
                weaponType = Math.min(3, weaponType + 1);
                break;
            case 3: // 得分加成
                score += 500;
                break;
        }
    }

    private void checkLevelUp() {
        if (enemyCount >= enemiesToNextLevel) {
            level++;
            enemyCount = 0;
            enemiesToNextLevel += 10; // 每级需要击败更多敌机

            // 升级奖励
            lives = Math.min(3, lives + 1);
            score += level * 1000;
        }
    }

    private void playSound(String soundKey) {
        if (sounds.containsKey(soundKey)) {
            Clip clip = sounds.get(soundKey);
            if (clip.isRunning()) {
                clip.stop();
            }
            clip.setFramePosition(0);
            clip.start();
        }
    }

    private void shoot() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastShotTime < shotDelay) {
            return; // 射击冷却中
        }

        lastShotTime = currentTime;
        playSound("shoot");

        int bulletWidth = 5;
        int bulletHeight = 15;
        int damage = 1;
        boolean isLaser = false;

        switch (weaponType) {
            case 1: // 单发
                bullets.add(new Bullet(player.getX() + player.getWidth()/2 - bulletWidth/2,
                        player.getY(), bulletWidth, bulletHeight,
                        damage, false, Color.YELLOW));
                break;
            case 2: // 双发
                bullets.add(new Bullet(player.getX() + player.getWidth()/4 - bulletWidth/2,
                        player.getY(), bulletWidth, bulletHeight,
                        damage, false, Color.YELLOW));
                bullets.add(new Bullet(player.getX() + 3*player.getWidth()/4 - bulletWidth/2,
                        player.getY(), bulletWidth, bulletHeight,
                        damage, false, Color.YELLOW));
                break;
            case 3: // 激光
                bullets.add(new Bullet(player.getX() + player.getWidth()/2 - 10,
                        player.getY(), 20, 30,
                        2, true, Color.CYAN));
                break;
        }
    }

    private void resetGame() {
        player = new Player(WIDTH/2-25, HEIGHT-100, 50, 50);
        enemies.clear();
        bullets.clear();
        explosions.clear();
        powerUps.clear();
        score = 0;
        lives = 3;
        level = 1;
        enemyCount = 0;
        enemiesToNextLevel = 20;
        weaponType = 1;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        keys[keyCode] = true;

        // 通用按键(在所有游戏状态下有效)
        switch (keyCode) {
            case KeyEvent.VK_ESCAPE:
                if (gameState == PLAYING || gameState == PAUSED) {
                    gameState = MENU;
                }
                break;
            case KeyEvent.VK_P:
                if (gameState == PLAYING) {
                    gameState = PAUSED;
                } else if (gameState == PAUSED) {
                    gameState = PLAYING;
                }
                break;
        }

        // 根据游戏状态处理其他按键
        switch (gameState) {
            case MENU:
                handleMenuInput(keyCode);
                break;
            case PLAYING:
                handlePlayingInput(keyCode);
                break;
            case GAME_OVER:
                handleGameOverInput(keyCode);
                break;
            case PAUSED:
                handlePausedInput(keyCode);
                break;
        }
    }

    private void handleMenuInput(int keyCode) {
        switch (keyCode) {
            case KeyEvent.VK_SPACE:
                resetGame();
                gameState = PLAYING;
                break;
            case KeyEvent.VK_H:
                // 显示帮助
                break;
        }
    }

    private void handlePlayingInput(int keyCode) {
        switch (keyCode) {
            case KeyEvent.VK_SPACE:
                shoot();
                break;
            case KeyEvent.VK_1:
                weaponType = 1;
                break;
            case KeyEvent.VK_2:
                weaponType = 2;
                break;
            case KeyEvent.VK_3:
                weaponType = 3;
                break;
        }
    }

    private void handleGameOverInput(int keyCode) {
        switch (keyCode) {
            case KeyEvent.VK_R:
                resetGame();
                gameState = PLAYING;
                break;
        }
    }

    private void handlePausedInput(int keyCode) {
        // 暂停状态下只响应继续游戏和返回菜单的按键
    }

    @Override
    public void keyReleased(KeyEvent e) {
        keys[e.getKeyCode()] = false;
    }

    @Override
    public void keyTyped(KeyEvent e) {}
}

// 游戏对象类
class GameObject {
    protected int x, y, width, height;

    public GameObject(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, width, height);
    }

    public int getX() { return x; }
    public int getY() { return y; }
    public int getWidth() { return width; }
    public int getHeight() { return height; }

    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }

    public void move(int dx, int dy) {
        x += dx;
        y += dy;
    }
}

class Player extends GameObject {
    public Player(int x, int y, int width, int height) {
        super(x, y, width, height);
    }
}

abstract class Enemy extends GameObject {
    protected int type;
    protected int health;
    protected int scoreValue;
    protected Color color;

    public Enemy(int x, int y, int width, int height, int type, int health, int scoreValue, Color color) {
        super(x, y, width, height);
        this.type = type;
        this.health = health;
        this.scoreValue = scoreValue;
        this.color = color;
    }

    public abstract void update();

    public boolean hit(int damage) {
        health -= damage;
        return health <= 0;
    }

    public int getType() { return type; }
    public Color getColor() { return color; }
    public int getScoreValue() { return scoreValue; }
}

class NormalEnemy extends Enemy {
    private static final int SPEED = 2;

    public NormalEnemy(int x, int y, int width, int height) {
        super(x, y, width, height, 1, 1, 100, Color.RED);
    }

    @Override
    public void update() {
        y += SPEED;
    }
}

class FastEnemy extends Enemy {
    private static final int SPEED = 4;

    public FastEnemy(int x, int y, int width, int height) {
        super(x, y, width, height, 2, 1, 150, Color.GREEN);
    }

    @Override
    public void update() {
        y += SPEED;
    }
}

class BigEnemy extends Enemy {
    private static final int SPEED = 1;

    public BigEnemy(int x, int y, int width, int height, int health) {
        super(x, y, width, height, 3, health, 300, Color.ORANGE);
    }

    @Override
    public void update() {
        y += SPEED;
    }
}

class Bullet extends GameObject {
    private int speed;
    private int damage;
    private boolean isLaser;
    private Color color;

    public Bullet(int x, int y, int width, int height,
                  int damage, boolean isLaser, Color color) {
        super(x, y, width, height);
        this.speed = isLaser ? 10 : 7;
        this.damage = damage;
        this.isLaser = isLaser;
        this.color = color;
    }

    public void update() {
        y -= speed;
    }

    public int getDamage() { return damage; }
    public boolean isLaser() { return isLaser; }
    public Color getColor() { return color; }
}

class Explosion {
    private int x, y, width, height;
    private int frame;
    private static final int MAX_FRAMES = 10;

    public Explosion(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.frame = 0;
    }

    public void update() {
        frame++;
    }

    public void draw(Graphics g) {
        // 简单的爆炸动画效果
        float progress = (float)frame / MAX_FRAMES;
        int alpha = (int)(255 * (1 - progress));
        int size = (int)(width * (1 + progress));

        g.setColor(new Color(255, 165, 0, alpha));
        g.fillOval(x + width/2 - size/2, y + height/2 - size/2, size, size);

        g.setColor(new Color(255, 255, 0, alpha));
        g.fillOval(x + width/2 - size/3, y + height/2 - size/3, size*2/3, size*2/3);
    }

    public boolean isFinished() {
        return frame >= MAX_FRAMES;
    }
}

class PowerUp extends GameObject {
    private static final int SPEED = 3;

    public PowerUp(int x, int y, int width, int height) {
        super(x, y, width, height);
    }

    public void update() {
        y += SPEED;
    }
}
